/*
Realistic Unit Modification v4.0 Dialog 
v4.0.0.1 [ALPHA]
Concept & Development by Draper & Raps | [VRC]
*/

/* variables defined */
_arry = _this;
_heloname = _arry select 0;
_helo = _arry select 0;
_callsign = _arry select 1;
_home = _arry select 2;
_airport = _arry select 3;
_egress = _arry select 4;
_ingress = _arry select 5;
_class = typeof _helo;
rumLZPos = nil;
_cont = format ["%1",_helo];
_end = format ["%1",_helo];
_vehCrew = [];
_crewCount = nil;
_crew = [];

RUM_THEL_AbortRTB = false;
RUM_Abort = nil;
RUM_THel_endflight = nil;
RUM_THel_land = nil;

if (_helo getVariable "RUM_inFlight") exitWith {hint "Helo in flight";};

/////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////// Start of Map selection /////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
RUM_ASclick = false;

openMap [true,false];
for [{_n = 1},{_n <= 6},{_n = _n + 1}] do {
	_mkrname = "";
	_type = "";
	_hint = "";
	switch (_n) do {
		case 1: {
			_mkrname = format ["%1target",_helo];
			_type = "mil_pickup";
			_hint = "Select Pick-Up LZ";
		};
		case 2: {
			_mkrname = format ["%1ingress",_helo];
			_type = "mil_start";
			_hint = "Select Pick-Up LZ Ingress Point";
		};
		case 3: {
			_mkrname = format ["%1egress",_helo];
			_type = "mil_end";
			_hint = "Select Pick-Up LZ Egress Point";
		};
		case 4: {
			_mkrname = format ["%1target2",_helo];
			_type = "mil_pickup";
			_hint = "Select Drop-Off LZ";
		};
		case 5: {
			_mkrname = format ["%1ingress2",_helo];
			_type = "mil_start";
			_hint = "Select Drop-Off LZ Ingress Point";
		};
		case 6: {
			_mkrname = format ["%1egress2",_helo];
			_type = "mil_end";
			_hint = "Select Drop-Off LZ Egress Point";
		};
		default {
			hint "oops!";	
		};
	};
	_asloc = createMarkerLocal [_mkrname, [0,0,0]];
	_asloc setMarkerShapeLocal "ICON";
	_mkrname setMarkerTypeLocal _type;
	_mkrname setMarkerAlphaLocal .35;
	
	_mapclick = ["rum_mapclick_id", "onMapSingleClick", { _this setMarkerPosLocal _pos;RUM_ASclick = true; }, _mkrname] call BIS_fnc_addStackedEventHandler;
	
	titleText [_hint, "PLAIN DOWN"];
	
	waitUntil {
		(RUM_ASclick || !(visiblemap))
	};
	
	if (!visibleMap) exitwith {
		RUM_Abort = true;
		systemChat "Insert Operation Aborted";
		_stgtar = nil;
		_mapclick = ["rum_mapclick_id", "onMapSingleClick"] call BIS_fnc_removeStackedEventHandler;
		RUM_ASclick = false;
		/* delete all the markers that were created for this instance. */
		_mkrname1 = format ["%1ingress",_helo];
		_mkrname2 = format ["%1target",_helo];
		_mkrname3 = format ["%1egress",_helo];
		_mkrname4 = format ["%1ingress2",_helo];
		_mkrname5 = format ["%1target2",_helo];
		_mkrname6 = format ["%1egress2",_helo];
		deletemarkerLocal _mkrname1;
		deletemarkerLocal _mkrname2;
		deletemarkerLocal _mkrname3;
		deletemarkerLocal _mkrname4;
		deletemarkerLocal _mkrname5;
		deletemarkerLocal _mkrname6;
		RUM_THelAircraft = RUM_THelAircraft + [_arry];
		_helo setVariable ["RUM_inFlight", false, true];
	};
		
	if((_n == 2) or (_n == 3)) then {
		_mkrname2 = format ["%1target",_helo];
		_mkrname setMarkerDirLocal ((getMarkerPos _mkrname2 select 0)-(getMarkerPos _mkrname select 0)) atan2 ((getMarkerPos _mkrname2 select 1)-(getMarkerPos _mkrname select 1));
	};
	if((_n == 5) or (_n == 6)) then {
		_mkrname2 = format ["%1target2",_helo];
		_mkrname setMarkerDirLocal ((getMarkerPos _mkrname2 select 0)-(getMarkerPos _mkrname select 0)) atan2 ((getMarkerPos _mkrname2 select 1)-(getMarkerPos _mkrname select 1));
	};
	_mapclick = ["rum_mapclick_id", "onMapSingleClick"] call BIS_fnc_removeStackedEventHandler;
	RUM_ASclick = false;
};

_wpPos1 = getPos _egress;
_wpPos2 = getMarkerPos format ["%1ingress",_helo];
_wpPos3 = getMarkerPos format ["%1target",_helo];
_wpPos4 = getMarkerPos format ["%1egress",_helo];
_wpPos5 = getMarkerPos format ["%1ingress2",_helo];
_wpPos6 = getMarkerPos format ["%1target2",_helo];
_wpPos7 = getMarkerPos format ["%1egress2",_helo];
_wpPos8 = getPos _ingress;
_wpPos9 = getPos _home;

titleFadeOut 0;
sleep 1;
openMap [false,false];

/////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// Start of Helo Setup //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
if (!isNil "RUM_Abort") exitWith {};

/* set the helo active in the PDA so it cannot be selected again and broadcasts MP */
_helo setVariable ["RUM_inFlight", true, true];

/* Unlocks the vehicle positions */
_helo lockDriver false; // Pilot
_helo lockTurret [[0], false]; // Co-Pilot
_helo lockTurret [[1], false]; // Gunner1 - if the vehicle has this position
_helo lockTurret [[2], false]; // Gunner2 - if the vehicle has this position

/* create units to fly helo  */
_grp = createGroup side player;
_pilot = _grp createUnit [RUM_THEL_Pilot, getpos _home, [], 10, "NONE"];
_commander = _grp createUnit [RUM_THEL_Pilot, getpos _home, [], 10, "NONE"];
_gunner1 = _grp createUnit [RUM_THEL_Pilot, getpos _home, [], 10, "NONE"];
_gunner2 = _grp createUnit [RUM_THEL_Pilot, getpos _home, [], 10, "NONE"];

/* count turret positions */
_cfg = configFile >> "CfgVehicles" >> typeof _helo >> "turrets";

_pilot moveInDriver _helo;
_commander moveinTurret [_helo,[0]];
_gunner1 moveinTurret [_helo,[1]];
_gunner2 moveinTurret [_helo,[2]];

if (count _cfg <= 1) then {
	deleteVehicle _gunner1;
	deleteVehicle _gunner2;
};

/* AI Crew Optimization */
{
	// _x disableAi "FSM";
	// _x disableAi "TARGET";
	// _x disableAi "AUTOTARGET";
	_x setSkill 1;
} forEach units _grp;

/* disables AI programming for VCOM AI */
{
	_x setVariable ["NOAI",1,false];
} forEach (units _grp);

/* Over Water Check - For Carrier Bases */
if (surfaceIsWater (getpos _home)) then {

	/* something here - WIP */
};

/* vehicle specific features */
_vehOptions = [_helo] spawn FNC_H_rumVehicleOptions;

/////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// Script Start ///////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
["shuffle",[player,_pilot,_callsign]] execVM (RUM_PATH+"Transport\radiodialogs.sqf");
sleep random(20);

/* - Smoke section - WIP
if (RUM_THEL_smokeLZ) then {
	[_helo] spawn {
		_mkrname = format ["%1target",_this];
		_asloc = getMarkerPos _mkrname;
		asloc = getMarkerPos _mkrname;
		waitUntil{
			sleep 0.15;
			((((getPos _this select 0) - (_asloc select 0)) ^ 2 + ((getPos _this select 1) - (_asloc select 1)) ^ 2) ^ 0.5) < 1500;
		};
		_smoke = createVehicle ["SmokeShell", _asloc, [], 0, "NONE"];
	};
};
*/

/* Start the Engine */
_helo engineOn true;

/* locks the vehicle positions so that other units cannot get in them */
_helo lockDriver true; // Pilot
_helo lockTurret [[0], true]; // Co-Pilot
_helo lockTurret [[1], true]; // Gunner1 - if the vehicle has this position
_helo lockTurret [[2], true]; // Gunner2 - if the vehicle has this position

/* store crew count for use in other scripts/mission types */
_crewCount = (count units _helo);
_vehCrew = (units _helo);
_vehGroup = group _helo;

/* setup variable for helo crew to be used in other functions */
_helo setVariable ["_crewVariables",[_crewCount,_vehCrew],true];

/* Assigns first set of waypoints to the pilot */
[_helo,_grp,_wpPos1] call FNC_H_rumTakeOff;
[_helo,_grp,_wpPos2] call FNC_H_rumMoveWP;
[_helo,_grp,_wpPos3] call FNC_H_rumLandLZ;

/* waits till objective position has been reached */
// waitUntil {
	// sleep 0.15;
	// ((!alive _helo) || (RUM_THel_land == _cont));
// };

/* lands the helo and leaves engines on */
_helo land "get in";

/* define variables used for pilot control */
_stg = format ["%1 : Move On!",_callsign];
_stg2 = format ["%1 : Abort, RTB!",_callsign];
_helo setVariable ["RUM_THel_continue",false,true];

/* loads the action menu */
if (alive _helo) then {
	RUM_THel_actionid = player addAction [_stg, (RUM_PATH+"Transport\om.sqf"),[_helo,_callsign],1,false,true,"","vehicle player == player"];
	RUM_THel_actionid2 = _helo addAction [_stg, (RUM_PATH+"Transport\om.sqf"),[_helo,_callsign],1,false,true,"","_this in _target"];
	// RUM_THel_actionid3 = player addAction [_stg2, (RUM_PATH+"Transport\abort.sqf"),[_arry]];
};

/* keeps the helo on the gorund til the player is ready */
_crewMonitor1 = [_helo] spawn {
	_helo = _this select 0;
	_helo_height = 0;
	waitUntil {
		sleep 0.5;
		if (surfaceIsWater getPos _helo) then {
			_helo_height =  getPosASL _helo select 2;
		} else {
			_helo_height =  getPosATL _helo select 2;
			if ((round _helo_height) >= 5) then {
				_helo flyinheight -1;
				_helo domove getPosATL _helo;
			};
		};
		
		// hintSilent format [ "%1's current altitude is: %2",_helo, _helo_height ];
		((!alive _helo) || (_helo getVariable "RUM_THel_continue"));
	};
	// hintC "Exited Crew Monitor 1 Successfully";
};

/* wait till the pilot is cleared to move on */
waitUntil{
	sleep 0.025;
	((!alive _helo) || (_helo getVariable "RUM_THel_continue"));
};

sleep 3;

/* delete the invisible helo pad used for the objective location landing */
if (RUM_THEL_invisibleH) then {
	deleteVehicle rumLZPos;
};

/* cancels landing action */
_helo land "NONE";

/* Build waypoints */
[_helo,_grp,_wpPos4] call FNC_H_rumTakeOff;
[_helo,_grp,_wpPos5] call FNC_H_rumMoveWP;

/* Features for insert behavior */
switch (RUM_THel_Special) do {
	case "Normal":{
		[_helo,_grp,_wpPos6] call FNC_H_rumLandLZ;
		_helo land "get out";
		_stg = format ["%1 : Move On!",_callsign];
		_helo setVariable ["RUM_THel_continue",false,true];

		if (alive _helo) then {
			RUM_THel_actionid = player addAction [_stg, (RUM_PATH+"Transport\om.sqf"),[_helo,_callsign],1,false,true,"","_this == vehicle _this;"];
			RUM_THel_actionid2 = _helo addAction [_stg, (RUM_PATH+"Transport\om.sqf"),[_helo,_callsign],1,false,true,"","_this in _target"];
		};

		/* keeps the helo on the gorund til the player is ready */
		_crewMonitor1 = [_helo] spawn {
			_helo = _this select 0;
			_helo_height = 0;
			waitUntil {
				sleep 0.5;
				if (surfaceIsWater getPos _helo) then {
					_helo_height =  getPosASL _helo select 2;
				} else {
					_helo_height =  getPosATL _helo select 2;
				};
				
				if ((round _helo_height) >= 5) then {
					_helo flyinheight -1;
					_helo domove getPosATL _helo;
				};
				// hintSilent format [ "%1's current altitude is: %2",_helo, _helo_height ];
				((!alive _helo) || (_helo getVariable "RUM_THel_continue"));
			};
			// hintC "Exited Crew Monitor 1 Successfully";
		};

		/* Wait till the helo has landed and player is ready to move on */
		// waitUntil {
			// sleep 0.15;
			// ((!alive _helo) || (RUM_THel_land == _cont));
		// };
	};
	
	case "Combat Landing":{
		[_helo,_grp,_wpPos6] call FNC_H_rumLandLZ;
		_helo setVariable ["RUM_THel_continue",false,true];
		[_helo] spawn FNC_H_rumSpecial_CombatLanding;
	};
	
	case "Fast Rope":{
		[_helo,_grp,_wpPos6] call FNC_H_ApproachWP;

		/* needs to have the A3 RavLifter Addon to work */
		_helo setVariable ["RUM_THel_continue",false,true];
		_helo setvariable ["fastRopePos", _wpPos6];
		_helo SetVariable ["fastRopeHeight", 30];
		_helo setvariable ["fastRopeOn", true];

		/* waits till the fast rope is complete */
		waituntil {
			sleep 0.025;
			if (!(_helo GetVariable "fastRopeOn")) then {
				sleep 3.5;
				_helo setVariable ["RUM_THel_continue",true,true];
				true;
			};
		};
		
	};
	case "Para Drop":{
		_crew = crew _helo;
		_cargo = ((_crew - units _grp) - units _grp2);
		{_x addBackpack "B_Parachute"} forEach _cargo;
		{unassignvehicle _x;_x action ["EJECT",_helo];sleep 0.25;} forEach _cargo;
		_helo setVariable ["RUM_THel_continue",true,true];
	};
	case "HALO Jump":{
		_crew = crew _plane;
		_cargo = ((_crew - units _grp) - units _grp2);
		{_x addBackpack "B_Parachute"} forEach _cargo;
		_plane spawn {sleep 1;_this setVariable ["RUM_THel_continue",true,true]};
		cutText ["This is the Drop Zone!","PLAIN",2];
	};	
	default{hint "shit"};
};

waitUntil{
	sleep 0.025;
	((!alive _helo) || (_helo getVariable "RUM_THel_continue"));
};

/* cancels landing action */
_helo land "NONE";

/* generate waypoints for the helop */
[_helo,_grp,_wpPos7] call FNC_H_rumDustOff;
[_helo,_grp,_wpPos8] call FNC_H_rumMoveWP;
[_helo,_grp,_wpPos9] call FNC_H_rumLandHome;

/* end of flight check when helo has landed */
waitUntil{
	sleep 0.15;
	((!alive _helo) || (RUM_THel_endflight == _end));
};

if (!alive _helo) exitwith {};

/* calls cleanup function once helo has returned to base */
[_helo,_home,_pilot] call FNC_H_rumEndMission;

/* Terminate spawn for vehicle options */
terminate _vehOptions;

/* waits till crew are deleted */
waitUntil {
	sleep 0.5;
	(!alive _helo || ((_helo getVariable ["_crewVariables",[]] select 0) == 0));
};

if (!alive _helo) exitwith {};

hintSilent "";
_helo setVariable ["_crewVariables",nil];
_helo addEventHandler ["HandleDamage", {true}];

sleep RUM_THEL_ReSupTime;

RUM_THelAircraft = RUM_THelAircraft + [_arry];
_helo setVariable ["RUM_inFlight", false, true];

if (true) exitwith {};